home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / src_d2.zoo / source / filemode.c < prev    next >
C/C++ Source or Header  |  1991-12-02  |  5KB  |  196 lines

  1. /* Examine the result of  stat  and make a string describing file modes.
  2.    Copyright (C) 1985 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. In other words, you are welcome to use, share and improve this program.
  19. You are forbidden to forbid anyone else to use, share and improve
  20. what you give them.   Help stamp out software-hoarding!  */
  21.  
  22.  
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25.  
  26. /* filemodestring - set file attribute data 
  27.  
  28. *** WARNING!  FILE STRUCTURE DEPENDENT ***
  29.  
  30.    Filemodestring converts the data in the st_mode field of file status
  31. block `s' to a 10 character attribute string, which it stores in
  32. the block that `a' points to.
  33. This attribute string is modelled after the string produced by the Berkeley ls.
  34.  
  35. As usual under Unix, the elements of the string are numbered
  36. from 0.  Their meanings are:
  37.  
  38.    0    File type.  'd' for directory, 'c' for character
  39.     special, 'b' for block special, 'm' for multiplex,
  40.     'l' for symbolic link, 's' for socket, 'p' for fifo,
  41.     '-' for any other file type
  42.  
  43.    1    'r' if the owner may read, '-' otherwise.
  44.  
  45.    2    'w' if the owner may write, '-' otherwise.
  46.  
  47.    3    'x' if the owner may execute, 's' if the file is
  48.     set-user-id, '-' otherwise.
  49.     'S' if the file is set-user-id, but the execute
  50.     bit isn't set.  (sys v `feature' which helps to
  51.     catch screw case.)
  52.  
  53.    4    'r' if group members may read, '-' otherwise.
  54.  
  55.    5    'w' if group members may write, '-' otherwise.
  56.  
  57.    6    'x' if group members may execute, 's' if the file is
  58.     set-group-id, '-' otherwise.
  59.     'S' if it is set-group-id but not executable.
  60.  
  61.    7    'r' if any user may read, '-' otherwise.
  62.  
  63.    8    'w' if any user may write, '-' otherwise.
  64.  
  65.    9    'x' if any user may execute, 't' if the file is "sticky"
  66.     (will be retained in swap space after execution), '-'
  67.     otherwise.
  68.  
  69.  */
  70.  
  71. #define VOID void
  72.  
  73. static char ftypelet ();
  74. static VOID rwx (), setst ();
  75.  
  76. VOID
  77. filemodestring (s,a)
  78.    struct stat    *s;
  79.    char *a;
  80. {
  81.    a[0] = ftypelet (s);
  82.    /* Aren't there symbolic names for these byte-fields? */
  83.    rwx ((s->st_mode & 0700) << 0, &(a[1]));
  84.    rwx ((s->st_mode & 0070) << 3, &(a[4]));
  85.    rwx ((s->st_mode & 0007) << 6, &(a[7]));
  86.    setst (s->st_mode, a);
  87. }
  88.  
  89. /* ftypelet - file type letter
  90.  
  91. *** WARNING!  FILE STRUCTURE DEPENDENT ***
  92.  
  93.    Ftypelet accepts a file status block and returns a character
  94. code describing the type of the file.  'd' is returned for
  95. directories, 'b' for block special files, 'c' for character
  96. special files, 'm' for multiplexor files, 'l' for symbolic link,
  97. 's' for socket, 'p' for fifo, '-' for any other file type
  98.  */
  99.  
  100. static char
  101. ftypelet(s)
  102.    struct stat *s;
  103. {
  104.   switch (s->st_mode & S_IFMT)
  105.     {
  106.     default:
  107.       return '-';
  108.     case S_IFDIR:
  109.       return 'd';
  110. #ifdef S_IFLNK
  111.     case S_IFLNK:
  112.       return 'l';
  113. #endif
  114. #ifdef S_IFCHR
  115.     case S_IFCHR:
  116.       return 'c';
  117. #endif
  118. #ifdef S_IFBLK
  119.     case S_IFBLK:
  120.       return 'b';
  121. #endif
  122. #ifdef S_IFMPC
  123. /* These do not seem to exist */
  124.     case S_IFMPC:
  125.     case S_IFMPB:
  126.       return 'm';
  127. #endif
  128. #ifdef S_IFSOCK
  129.     case S_IFSOCK:
  130.       return 's';
  131. #endif
  132. #ifdef S_IFIFO
  133. #if S_IFIFO != S_IFSOCK
  134.     case S_IFIFO:
  135.       return 'p';
  136. #endif
  137. #endif
  138. #ifdef S_IFNWK /* hp-ux hack */
  139.     case S_IFNWK:
  140.       return 'n';
  141. #endif
  142.     }
  143. }
  144.  
  145.  
  146. /* rwx - look at read, write, and execute bits and set character
  147. flags accordingly
  148.  
  149. *** WARNING!  FILE STRUCTURE DEPENDENT ***
  150.  
  151.  */
  152.  
  153. static VOID
  154. rwx (bits, chars)
  155.    unsigned short bits;
  156.    char chars[];
  157. {
  158.   chars[0] = (bits & S_IREAD)  ? 'r' : '-';
  159.   chars[1] = (bits & S_IWRITE) ? 'w' : '-';
  160.   chars[2] = (bits & S_IEXEC)  ? 'x' : '-';
  161. }
  162.  
  163.  
  164. /* setst - set s & t flags in a file attributes string */
  165. /* *** WARNING!  FILE STRUCTURE DEPENDENT *** */
  166. static VOID
  167. setst (bits, chars)
  168.    unsigned short bits;
  169.    char chars[];
  170. {
  171. #ifdef S_ISUID
  172.    if (bits & S_ISUID)
  173.      {
  174.        if (chars[3] != 'x')
  175.      /* Screw case: set-uid, but not executable. */
  176.      chars[3] = 'S';
  177.        else
  178.      chars[3] = 's';
  179.      }
  180. #endif
  181. #ifdef S_ISGID
  182.    if (bits & S_ISGID)
  183.      {
  184.        if (chars[6] != 'x')
  185.      /* Screw case: set-gid, but not executable. */
  186.      chars[6] = 'S';
  187.        else
  188.      chars[6] = 's';
  189.      }
  190. #endif
  191. #ifdef S_ISVTX
  192.    if (bits & S_ISVTX)
  193.       chars[9] = 't';
  194. #endif
  195. }
  196.